package com.vegetable.modules.controller.sys;

import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.common.utils.ValidatorUtils;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.sys.SysRoleEntity;
import com.vegetable.modules.service.sys.SysRoleMenuService;
import com.vegetable.modules.service.sys.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/role")
@AllArgsConstructor
@Api(value = "角色管理", tags = "角色管理")
public class SysRoleController extends AbstractController {
	private SysRoleService sysRoleService;
	private SysRoleMenuService sysRoleMenuService;

	/**
	 * 角色列表
	 */
	@GetMapping("/list")
	@ApiOperation(value = "条件查询角色列表", notes = "条件查询角色列表")
	public R list(@RequestParam Map<String, Object> params){
		//如果不是超级管理员，则只查询自己创建的角色列表
		/*if(getUserId() != Constant.SUPER_ADMIN){
			params.put("createUserId", getUserId());
		}*/

		PageUtils page = sysRoleService.queryPage(params);

		return R.data(page);
	}
	
	/**
	 * 角色列表
	 */
	@GetMapping("/select")
	@ApiOperation(value = "全部角色列表", notes = "全部角色列表")
	public R select(){
		Map<String, Object> map = new HashMap<>();
		
		//如果不是超级管理员，则只查询自己所拥有的角色列表
		/*if(getUserId() != Constant.SUPER_ADMIN){
			map.put("create_user_id", getUserId());
		}*/
		List<SysRoleEntity> list = sysRoleService.listByMap(map);
		
		return R.data(list);
	}
	
	/**
	 * 角色信息
	 */
	@GetMapping("/info")
	@ApiOperation(value = "角色信息", notes = "角色信息")
	public R info(Long roleId){
		SysRoleEntity role = sysRoleService.getById(roleId);
		
		//查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
		role.setMenuIdList(menuIdList);
		
		return R.data(role);
	}
	
	/**
	 * 保存角色
	 */
	@SysLog(value = "保存角色")
	@PostMapping("/save")
	@ApiOperation(value = "保存角色", notes = "保存角色")
	public R save(@RequestBody SysRoleEntity role) throws Exception {
		ValidatorUtils.validateEntity(role);
		
		role.setCreateUserId(getUserId());
		sysRoleService.saveRole(role);
		
		return R.status(true);
	}
	
	/**
	 * 修改角色
	 */
	@SysLog(value = "修改角色")
	@PostMapping("/update")
	@ApiOperation(value = "修改角色", notes = "修改角色")
	public R update(@RequestBody SysRoleEntity role) throws Exception {
		ValidatorUtils.validateEntity(role);
		
		role.setCreateUserId(getUserId());
		sysRoleService.update(role);
		
		return R.status(true);
	}
	
	/**
	 * 删除角色
	 */
	@SysLog(value = "删除角色")
	@GetMapping("/delete")
	@ApiOperation(value = "删除角色", notes = "删除角色")
	public R delete(Long id){
		sysRoleService.removeById(id);
		return R.status(true);
	}
}
